package ru.alperez.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import ru.alperez.model.ExtendedLocation;

public class Converter {
	private static SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy   HH:mm:ss.S");
	{
		dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
	}
	
	
	public static String gpsAccuracyToString (float accuracy) {
		return String.format("%.1f m", accuracy);
	}
	
	public static String gpsDateTimeFromLocation(ExtendedLocation location) {
		if (location == null) return "";
		return dateFormat.format(new Date(location.getTime()));
	}
	
	public static String gpsLatitudeDMSFromLocation (ExtendedLocation location) {
		if (location == null) return "";
		double lat = location.getLatitude();
		int latDegs = (int)lat;
		int latMins = (int)((lat - latDegs) * 60);
		float latSecs = (float)(lat - latDegs - ((double)latMins)/60.0) * 3600;
		return String.format("%03d%c %02d\' %.2f\"%s", Math.abs(latDegs), (short)176, Math.abs(latMins), Math.abs(latSecs), (lat >= 0) ? "N" : "S");
	}
	
	public static String gpsLongtitudeDMSFromLocation (ExtendedLocation location) {
		if (location == null) return "";
		double lon = location.getLongitude();
		int lonDegs = (int)lon;
		int lonMins = (int)((lon - lonDegs) * 60);
		float lonSecs = (float)(lon - lonDegs - ((double)lonMins)/60.0) * 3600;
		return String.format("%03d%c %02d\' %.2f\"%s", Math.abs(lonDegs), (short)176, Math.abs(lonMins), Math.abs(lonSecs), (lon >= 0) ? "E" : "W");
	}
	
	public static String gpsLatitudeDMSFromDegrees (double lat) {
		int latDegs = (int)lat;
		int latMins = (int)((lat - latDegs) * 60);
		float latSecs = (float)(lat - latDegs - ((double)latMins)/60.0) * 3600;
		return String.format("%03d%c %02d\' %.2f\"%s", Math.abs(latDegs), (short)176, Math.abs(latMins), Math.abs(latSecs), (lat >= 0) ? "N" : "S");
	}
	
	public static String gpsLongtitudeDMSFromDegrees (double lon) {
		int lonDegs = (int)lon;
		int lonMins = (int)((lon - lonDegs) * 60);
		float lonSecs = (float)(lon - lonDegs - ((double)lonMins)/60.0) * 3600;
		return String.format("%03d%c %02d\' %.2f\"%s", Math.abs(lonDegs), (short)176, Math.abs(lonMins), Math.abs(lonSecs), (lon >= 0) ? "E" : "W");
	}
	
	public static String gpsDegrees2String (double degrees) {
		return String.format("%.7f%c", degrees, (short)176);
	}
	
	public static String gpsAltitudeFromLocation(ExtendedLocation location) {
		if (location==null) return "";
		return String.format("%.1fm", location.getAltitude());
	}
	
	public static String doubleDegrees2String (double degrees) {
		return String.format("%.7f", degrees);
	}
	
	public static String floatAltitude2String (float alt) {
		return String.format("%.1f", alt);
	}
	
	/**
	 * Extracts date value from Date object
	 * @param date Date object
	 * @return Extracted date
	 */
	public static int dDate2Int(Date date) {
		return date == null ? 0 : (int) (Long.valueOf(String.format("%ts", date)) / 86400 /*24*60*60*/);
	}
	
	/**
	 * Extracts time value in seconds precision (int) from Date object
	 * @param date Date object
	 * @return Extracted time (seconds precision)
	 */
	public static int tDate2IntSeconds(Date date) {
		return date == null ? 0 : Integer.valueOf(String.format("%tH", date)) * 3600 + Integer.valueOf(String.format("%tM", date)) * 60 + Integer.valueOf(String.format("%tS", date));
	}
}
